package ptu.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ptu.dao.BookDao;
import ptu.entity.Book;
import ptu.result.Result;
import ptu.service.BookService;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookDao bookDao;
    @Override
    public Result selectAll() {
        List<Book> books = bookDao.selectAll();
        Result result = Result.success(books);
        return result;
    }

    @Override
    public Result addBook(Book book) {
        book.setBTime(LocalDateTime.now());
        int row = bookDao.insert(book);
        if(row<1){
            Result.error();
        }
        return Result.success();
    }

    @Override
    public Result update(Book book) {
        bookDao.update(book);
        return Result.success();

    }

    @Override
    public Result select(String work) {
        Book book = bookDao.select(work);
        if (book==null){
            return Result.error();
        }
        return Result.success(book);
    }

    @Override
    public Result delete(String bName) {
        int row = bookDao.delete(bName);
        if(row<1){
            return Result.error();
        }
        return Result.success();
    }

    @Override
    public Result queryLike(Book book) {

        List<Book> books = bookDao.selectLike(book);

        return Result.success(books);
    }

    @Override
    public Book getBookById(Integer id) {
        return bookDao.getBookById(id);
    }


}
